<?php

namespace app\common\model;

use think\Model;

class UserModel extends Model
{
    protected $table = 'edu_user';

    public $fields = [
      'id'=>'id',
      'pid'=>'pid',
      'realname'=>'realname',
      'openid'=>'openid',
      'gender'=>'gender',
      'avatar'=>'avatar',
      'role'=>'role',
      'school_id'=>'school_id',
      'school_name'=>'school_name',
      'grade_id'=>'grade_id',
      'class_id'=>'class_id',
      'student_no'=>'student_no',
      'pwd'=>'pwd',
      'teacher_no'=>'teacher_no',
      'phone'=>'phone',
      'create_time'=>'create_time',
    ];
    
    public function school(){
        return $this->belongsTo('SchoolModel','school_id');
    }

    public function schoolgrade(){
        return $this->belongsTo('SchoolGradeModel','grade_id');
    }

    public function schoolclass(){
        return $this->belongsTo('SchoolClassModel','class_id');
    }

    public function UserTimes(){
        return $this->belongsTo('UserTimesModel','id')
            ->field('group_concat(course_id) as course_ids');
    }

    /**
     * 学生ID
     */
    public function getUserIdAttr($val,$row){
        $user_id = Db::table('edu_user')->where('id',$row['id'])->find()['id'];
        return $user_id;
    }
}